package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Course;
import cn.wolfcode.crm.query.CourseQueryObject;
import cn.wolfcode.crm.service.ICourseService;
import cn.wolfcode.crm.util.JSONResult;
import cn.wolfcode.crm.util.PageResult;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("course")
public class CourseController {

    @Autowired
    ICourseService courseService;

    @RequestMapping("")
    @RequiresPermissions(value = {"课程首页", "course:index"}, logical = Logical.OR)
    public String index() {

        return "course/list";
    }

    @RequestMapping("list")
    @RequiresPermissions(value = {"课程列表", "course:list"}, logical = Logical.OR)
    @ResponseBody
    public PageResult list(CourseQueryObject qo) {
        PageResult result = courseService.query(qo);
        return result;
    }

    @RequestMapping("listData")
    @ResponseBody
    public Object listData() {

        return courseService.list();
    }

    @RequestMapping("save")
    @RequiresPermissions(value = {"课程保存", "course:save"}, logical = Logical.OR)
    @ResponseBody
    public JSONResult save(Course entity) {
        JSONResult result = new JSONResult();
        try {
            courseService.save(entity);
        } catch (Exception e) {
            e.printStackTrace();
            result.remark("操作失败");
        }
        return result;
    }

    @RequestMapping("update")
    @RequiresPermissions(value = {"课程更新", "course:update"}, logical = Logical.OR)
    @ResponseBody
    public JSONResult update(Course entity) {
        JSONResult result = new JSONResult();
        try {
            courseService.update(entity);
        } catch (Exception e) {
            e.printStackTrace();
            result.remark("操作失败");
        }
        return result;
    }


    @RequestMapping("delete")
    @RequiresPermissions(value = {"课程删除", "course:delete"}, logical = Logical.OR)
    @ResponseBody
    public JSONResult delete(Long id) {
        JSONResult result = new JSONResult();
        try {
            courseService.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
            result.remark("操作失败");
        }
        return result;
    }

    @RequestMapping("query")
    @ResponseBody
    public PageResult query(CourseQueryObject qo) {

        PageResult result = courseService.query(qo);

        return result;
    }

}